package com.itheima.motorcycletype.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.motorcycletype.dao.LogoMapper;
import com.itheima.motorcycletype.dao.MotorcycleTypeMapper;
import com.itheima.motorcycletype.pojo.Logo;
import com.itheima.motorcycletype.pojo.MotorcycleType;
import com.itheima.motorcycletype.service.LogoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class LogoServiceImpl implements LogoService {

    @Autowired
    private LogoMapper logoMapper;

    @Autowired
    private MotorcycleTypeMapper motorcycleTypeMapper;

    /**
     * 分页查询
     * @param map
     * @return
     */
    @Override
    public Page<Logo> selectAll(Map<String,Object> map) {
        Integer page=1;
        Integer size=10;
        if (map.get("page")!=null&&!"".equals(map.get("page"))){
            page= Integer.parseInt((String)map.get("page")) ;
        }
        if (map.get("size")!=null&&!"".equals(map.get("size"))){
            size= Integer.parseInt((String)map.get("size")) ;
        }
        PageHelper.startPage(page,size);
        return (Page<Logo>) logoMapper.selectAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Logo selectById(Integer id) {
        return logoMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param logo
     * @return
     */
    @Override
    public Integer add(Logo logo) {
       return logoMapper.insert(logo);
    }

    /**
     * 修改
     * @param logo
     * @return
     */
    @Override
    public Integer update(Logo logo) {
       return logoMapper.updateByPrimaryKey(logo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Integer delete(Integer id) {
       return logoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据品牌查询名字和编码
     * @param searchMap
     * @return
     */
    @Override
    public List<Logo> findList(Map searchMap) {
        Example example = createExample(searchMap);
        return logoMapper.selectByExample(example);
    }

    /**
     * 根据车型查询名字和编码
     * @param searchMap
     * @return
     */
    @Override
    public List<MotorcycleType> findListByMotorcycleType(Map searchMap) {
        Example example = createExampleByMotorcycleType(searchMap);
        return motorcycleTypeMapper.selectByExample(example);
    }


    /**
     * 构建查询品牌对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Logo.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if(searchMap.get("country")!=null && !"".equals(searchMap.get("country"))){
                criteria.andLike("country","%"+searchMap.get("country")+"%");
            }

        }
        return example;
    }

    /**
     * 构建查询车型对象
     * @param searchMap
     * @return
     */
    private Example createExampleByMotorcycleType(Map<String, Object> searchMap){
        Example example=new Example(MotorcycleType.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if(searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andLike("code","%"+searchMap.get("code")+"%");
            }
        }
        return example;
    }
}
